<?php
/**
 * insert row to table reward&punishment
 * @param $key
 * @return result
 */
function rnp_select($key=false, $orderby=false, $groupby=false){
	$sql = "SELECT * from rewarddanpunishment";
	if ($key){
		$sql .= " WHERE ( REWARD_ID LIKE '%$key%' OR
				REWARD_NAMA	like '%$key%' OR 
				REWARD_JENIS like '%$key%' )";
	}
	if ($groupby) $sql .= " GROUP BY $groupby ";
	if ($orderby) $sql .= " ORDER BY $orderby ";
	$result = mysql_query($sql);
	return !$result? mysql_error() : $result;
}

function rnp_select2($key=false, $where=false, $orderby=false, $groupby=false){
	$sql = "select * from rewarddanpunishment";
	if ($key || $where) $sql .= " where ";
	if ($key){
		$sql .= " ( REWARD_ID LIKE '%$key%' OR
				REWARD_NAMA	like '%$key%' OR 
				REWARD_JENIS like '%$key%' )";
	}
	if ($where) $sql .= $key? " AND $where " : " $where ";
	if ($groupby) $sql .= " GROUP BY $groupby ";
	if ($orderby) $sql .= " ORDER BY $orderby ";
	$result = mysql_query($sql);
	return !$result? mysql_error() : $result;
}

function rnp_generate_id(){
	$sql = "SELECT max(substring(`REWARD_ID`,2)) as max FROM `rewarddanpunishment` WHERE 1";
	$result = mysql_query($sql);
	$result = mysql_fetch_assoc($result);
	return 'R'.($result['max']+1);
}

/**
 * load row from table reward&punishment
 * @param $id
 * @return result
 */
function rnp_load($id){
	$sql = "select * from rewarddanpunishment
			where REWARD_ID = '$id'";
	return mysql_query($sql);
}

/**
 * check wheter the specific id is exist on the table reward&punishment
 * @param $id specific id
 * @return boolean value (true:exist, false:otherwise)
 */
function rnp_isExistID($id){
	$sql = "select * from rewarddanpunishment
			where REWARD_ID = '$id'";
	
	$result = mysql_query($sql);
	$result = mysql_fetch_assoc($result);
	if ($res['REWARD_ID'] === $id) return true;
	return false;
}

/**
 * insert a record to reward & punishment table
 * @param $id
 * @param $nama
 * @param $jenis
 * @return resu;t
 */
function rnp_add($id, $nama, $jenis){
	$sql = "insert into rewarddanpunishment ".
			"values ('$id','$nama', '$jenis')";
	return mysql_query($sql);
}

/**
 * update a row in reward & punishment table
 * @param $id
 * @param $nama
 * @param $jenis
 * @return result
 */
function rnp_update($id, $nama, $jenis){
	$sql = "update rewarddanpunishment ".
			"set REWARD_NAMA = '$nama',
				REWARD_JENIS = '$jenis'
			where REWARD_ID ='$id'";
	return mysql_query($sql);
}

/**
 * delete a row in reward & punishment table
 * @param $gol_id
 * @return boolean
 */
function rnp_delete($id){
	$sql = "delete from rewarddanpunishment ".
			"where REWARD_ID='$id'";
	return mysql_query($sql);
}

